/**
 * 姓名测算数据模型
 * 定义姓名测算结果的数据结构
 */

const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

/**
 * 姓名测算结果模型
 * @typedef {Object} NameAnalysis
 * @property {string} id - 唯一标识符 (UUID)
 * @property {string} name - 姓名
 * @property {Array<string>} positiveTraits - 积极特质列表
 * @property {Array<string>} growthSuggestions - 成长建议列表
 * @property {string} interpretation - 姓名解读
 * @property {Array<string>} positiveTags - 正能量标签
 * @property {string} userId - 用户ID (外键，关联User表)
 * @property {Date} createdAt - 创建时间
 * @property {Date} updatedAt - 更新时间
 */
const NameAnalysis = sequelize.define('NameAnalysis', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
    allowNull: false,
    comment: '姓名测算结果唯一标识符'
  },
  name: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '姓名'
  },
  positiveTraits: {
    type: DataTypes.JSON,
    allowNull: false,
    comment: '积极特质列表'
  },
  growthSuggestions: {
    type: DataTypes.JSON,
    allowNull: false,
    comment: '成长建议列表'
  },
  interpretation: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: '姓名解读'
  },
  positiveTags: {
    type: DataTypes.JSON,
    allowNull: false,
    comment: '正能量标签'
  },
  userId: {
    type: DataTypes.STRING(36), // UUID格式
    allowNull: true, // 允许为空，表示匿名用户
    comment: '用户ID (外键，关联User表)',
    field: 'user_id'
  }
}, {
  tableName: 'name_analyses',
  timestamps: true,
  indexes: [
    {
      fields: ['user_id'],
      name: 'idx_name_analyses_user_id'
    },
    {
      fields: ['name'],
      name: 'idx_name_analyses_name'
    },
    {
      fields: ['createdAt'],
      name: 'idx_name_analyses_created_at'
    }
  ],
  comment: '姓名测算结果表'
});

module.exports = NameAnalysis;